///日志级别
enum LogLevel{
  debug,//调试
  info,//信息
  waring,//警告
  error,//错误
  exception//异常
}

//日志类
class Log{

  //日式处理级别,默认Debug
  static LogLevel dealLevel = LogLevel.debug;

  //拦截日志处理，默认终端打印
  static Function(LogLevel level,Object message) onHandleLog = Log.printToConsole;

  static config(LogLevel level,{Function onHandleLog}){
    dealLevel = level;
    if(onHandleLog != null){
      onHandleLog = onHandleLog;
    }
  }


  static String levelDescrption(LogLevel level){
    String strDes = "";
    if(level == LogLevel.debug){
      strDes = "DEBUG";
    }else if(level == LogLevel.info){
      strDes = "INFO";
    }else if(level == LogLevel.waring){
      strDes = "WARNING";
    }else if(level == LogLevel.error){
      strDes = "ERROR";
    }else if(level == LogLevel.exception){
      strDes = "EXCEPTION";
    }
    return strDes;
  }

  //调试
  static void D(Object msg){
    _dealMessage(msg,LogLevel.debug);
  }

  //信息
  static void I(Object msg){
    _dealMessage(msg,LogLevel.info);
  }

  //警告
  static void W(Object msg){
    _dealMessage(msg,LogLevel.waring);
  }

  //错误
  static void Er(Object msg){
    _dealMessage(msg,LogLevel.error);
  }

  //异常
  static void Ex(Object msg){
    _dealMessage(msg,LogLevel.exception);
  }

  static void _dealMessage(Object msg,LogLevel level){
    if(dealLevel.index <= level.index){
      onHandleLog(level,msg);
    }
  }

  //终端输出
  static void printToConsole(LogLevel level,Object object,[ StackTrace stackTrace]){
    String msg = object.toString();
    if(dealLevel.index <= level.index){
      String time = dateAndTimeToString(DateTime.now(),formart: {"y-m":"-","m-d":"-","d-h":" ","h-m":":","m-s":":"});
      String str;
      if(level == LogLevel.exception){
        str = "$time[${levelDescrption(level)}]:$msg\n${StackTrace.current}";
      }else{
        str = "$time[${levelDescrption(level)}]:$msg";
      }
      print(str);
      print(stackTrace);
    }
  }

  /*  时间戳转字符串
  * timestamp 时间戳
  * formart ："y-m":年和月之间的符号,
  * "m-d":月和日之间的符号
  * "h-m":时和分之间的符号,
  * "m-s":分和秒之间的符号；
  * "m-a":是否显示上午和下午
  */
  static String dateAndTimeToString(DateTime dateTime, {Map<String, String> formart}) {
    if (dateTime == null ) {
      return "";
    }
    var timestamp = dateTime.microsecondsSinceEpoch;
    String targetString = "";
    final date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);
    // final String tmp = date.toString();
    String year = date.year.toString();
    String month = date.month.toString();
    if(date.month<=9){
      month = "0" + month;
    }
    String day = date.day.toString();
    if(date.day<=9){
      day = "0" + day;
    }
    String hour = date.hour.toString();
    if(date.hour<=9){
      hour = "0" + hour;
    }
    String minute = date.minute.toString();
    if(date.minute<=9){
      minute = "0" + minute;
    }
    String second = date.second.toString();
    if(date.second<=9){
      second = "0" + second;
    }
    // String millisecond = date.millisecond.toString();
    String morningOrafternoon = "上午";
    if(date.hour >= 12){
      morningOrafternoon = "下午";
    }

    if(formart["y-m"] != null && formart["m-d"] != null){
      targetString = year + formart["y-m"] + month + formart["m-d"] + day;
    }else if(formart["y-m"] == null && formart["m-d"] != null){
      targetString = month + formart["m-d"] + day;
    }else if(formart["y-m"] != null && formart["m-d"] == null){
      targetString = year + formart["y-m"] + month;
    }

    targetString += " ";

    if(formart["m-a"] != null){
      targetString += morningOrafternoon + " ";
    }

    if(formart["h-m"] != null && formart["m-s"] != null){
      targetString +=  hour + formart["h-m"] + minute + formart["m-s"] + second;
    }else if(formart["h-m"] == null && formart["m-s"] != null){
      targetString +=  minute + formart["m-s"] + second;
    }else if(formart["h-m"] != null && formart["m-s"] == null){
      targetString += hour + formart["h-m"] + minute;
    }
    return targetString;

  }

}